﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Job.Entity;
using Job.Logic.Personal;
using Job.Logic.Common;
using Job.Web.Framework;

namespace Job.Web.Common
{
    public partial class ResumeBrowse : BasePage
    {
        public int ResumeId
        {
            get { return myR["resumeid"].IntValue; }
        }
        public int categoryid
        {
            get { return myR["categoryid"].IntValue; }
        }
        public int send
        {
            get { return myR["send"].IntValue; }
        }

        public int resumeType
        {
            get { return myR["resumeType"].IntValue; }
        }

        public int resumeTypeURL
        {
            get
            {
                if (resumeType == 2)
                {
                    return 2;
                }
                else
                {
                    return 1;
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {            
            MySession session = new MySession();
            try
            {
                //如果是后台管理员查看投递到某个职位的简历
                if (session.GetAdminUserFromSession() != null)
                {
                    int sendId = myR["send"].IntValue;
                    if ( sendId > 0 )  new ApplyJobLogic().UpdateSendStatus(sendId, '2');
                }
            }
            catch (Exception ex)
            {
            }

            if (session.GetPersonalFromSession() == null)
            {
                ReturnError("您无权限查看个人简历！");
                return;
            }

            if (!IsPostBack)
            {
                try
                {
                    this.LoadData(resumeTypeURL);
                }
                catch (Exception ex)
                {
                    ReturnError("系统内部异常，请稍后再使用！");
                    return;
                }
            }
        }

        private void ReturnError(string error)
        {
            Response.Write("<script>\n");
            Response.Write(" alert('" + error + "');\n");
            Response.Write(" location.href='" + WebCommon.GetApplicationPath(Request) + "Index.aspx';\n");
            Response.Write("</script>");
        }

        private void LoadData(int resumeTypeParam)
        {
            
            int pid = ResumeId;
            if (pid <= 0)
            {
                ReturnError("请指定需要查看的简历！");
                return;
            }

            imgResume.ImageUrl = "/Images/resume.gif";
            PersonalLogic pl = new PersonalLogic();
            JobPersonInfo info = pl.GetSinglePersonBaseInfo(pid);
            if (null == info)
            {
                ReturnError("指定的个人简历已不存在！");
                return;
            }

            int accountId = new MySession().GetPersonalFromSession().PersonAccountid;
            if (accountId > 0 && accountId != info.PersonAccountId)
            {
                ReturnError("您无权限查看指定的简历！");
                return;
            }

            if (resumeTypeParam == 1)
            {
                AHRBROWSE.InnerText = "英文简历预览";
                AHRBROWSE.HRef = "ResumeBrowse.aspx?resumeid=" + this.ResumeId + "&categoryid=1&resumetype=2";
               
                TRENG.Visible = false;
                dlWork.DataSource = pl.GetWorksByPersonalId(pid);
                dlWork.DataBind();
                if (dlWork.Items == null || dlWork.Items.Count <= 0)
                {
                    TRWorkHeader.Visible = false;
                    TRWorkItem.Visible = false;
                }

                dlTran.DataSource = pl.GetTransByPersonalId(pid);
                dlTran.DataBind();
                if (dlTran.Items == null || dlTran.Items.Count <= 0)
                {
                    TRTranHeader.Visible = false;
                    TRTranItem.Visible = false;
                }
                dlEdu.DataSource = pl.GetEdusByPersonalId(pid);
                dlEdu.DataBind();
                if (dlEdu.Items == null || dlEdu.Items.Count <= 0)
                {
                    TReduHeader.Visible = false;
                    TReduItem.Visible = false;
                }
                if (info != null)
                {
                    lblName.Text = info.UserName == null ? "" : info.UserName;
                    int sex = info.UserSex.HasValue == true ? info.UserSex.Value : -1;
                    lblSex.Text = MemoryDictionary.GetCommonDictName(sex);
                    int nation = info.UserNationality.HasValue == true ? info.UserNationality.Value : -1;
                    lblNation.Text = MemoryDictionary.GetCommonDictName(nation);
                    string birthday = info.BirthDate.HasValue == true ? info.BirthDate.Value.ToString("yyyy-MM-dd") : "";
                    lblBirthday.Text = birthday;
                    int married = info.MarryState.HasValue == true ? info.MarryState.Value : -1;
                    lblMarried.Text = MemoryDictionary.GetCommonDictName(married);
                    lblTall.Text = (info.UserHeight.HasValue == true && info.UserHeight.Value > 0) ? info.UserHeight.Value.ToString() + "cm" : "";

                    lblHomeAdd.Text = MemoryDictionary.GetFullAddrName(info.HomeAddr);
                    lblNowAdd.Text = MemoryDictionary.GetFullAddrName(info.ResideAddr);
                    int lanType = info.ForeignLang.HasValue == true ? info.ForeignLang.Value : -1;
                    lblForLan.Text = MemoryDictionary.GetCommonDictName(lanType);
                    int forSkill = info.ForeignSkill.HasValue == true ? info.ForeignSkill.Value : -1;
                    lblForSkill.Text = MemoryDictionary.GetCommonDictName(forSkill);
                    int forcer = info.ForeignCertify.HasValue == true ? info.ForeignCertify.Value : -1;
                    lblForCer.Text = MemoryDictionary.GetCommonDictName(forcer);
                    int yueskill = info.DialectLang.HasValue == true ? info.DialectLang.Value : -1;
                    lblYueSkill.Text = MemoryDictionary.GetCommonDictName(yueskill);

                    lblOtherCer.Text = info.OtherCertify == null ? "" : info.OtherCertify;
                    lblWorkLen.Text = (info.WorkLen.HasValue == true && info.WorkLen.Value > 0) ? info.WorkLen.Value + "年" : "";

                    int _eduHei = info.HighEdu.HasValue == true ? info.HighEdu.Value : -1;
                    lblHeightEdu.Text = MemoryDictionary.GetCommonDictName(_eduHei);
                    string _spec = info.SpecialtyClass1 == null ? "" : info.SpecialtyClass1.Trim();
                    lblSpec.Text = MemoryDictionary.GetFullSpecialtyName(_spec);
                    lblZiGe.Text = info.WorkQualify == null ? "" : info.WorkQualify;
                    lblSchool.Text = info.GradSchoole;

                    int computerskill = info.ComputerSkill.HasValue == true ? info.ComputerSkill.Value : -1;
                    lblComputerSkill.Text = MemoryDictionary.GetCommonDictName(computerskill);
                    int zc = info.TechTitle.HasValue == true ? info.TechTitle.Value : -1;
                    lblZc.Text = MemoryDictionary.GetCommonDictName(zc);
                    string _industry = info.CurIndustry == null ? "" : info.CurIndustry.Trim();
                    //lblNowIndustry.Text = MemoryDictionary.GetFullIndustryName(_industry);

                    //lblObjectIndustry.Text = info.HopeIndustry == null ? "" : info.HopeIndustry.Trim();
                    string idInd = info.HopeIndustry == null ? "" : info.HopeIndustry.Trim();
                    if (idInd != "")
                    {
                        string[] idss = idInd.Split(",".ToCharArray());
                        string listText = "";
                        foreach (string id in idss)
                        {
                            listText += MemoryDictionary.GetFullIndustryName(id) + ",";
                        }
                        if (listText.EndsWith(","))
                            listText = listText.Substring(0, listText.Length - 1);
                        lblObjectIndustry.Text = listText;

                    }

                    string idAdd = info.HopeAddr == null ? "" : info.HopeAddr.Trim();
                    if (idAdd != "")
                    {
                        string[] idss = idAdd.Split(",".ToCharArray());
                        string listText = "";
                        foreach (string id in idss)
                        {
                            listText += MemoryDictionary.GetFullAddrName(id) + ",";
                        }
                        if (listText.EndsWith(","))
                            listText = listText.Substring(0, listText.Length - 1);
                        lblObjectAddress.Text = listText;

                    }

                    string idPos = info.HopePosition == null ? "" : info.HopePosition.Trim();
                    if (idPos != "")
                    {
                        string[] idss = idPos.Split(",".ToCharArray());
                        string listText = "";
                        foreach (string id in idss)
                        {
                            listText += MemoryDictionary.GetFullPositionClassName(id) + ",";
                        }
                        if (listText.EndsWith(","))
                            listText = listText.Substring(0, listText.Length - 1);
                        lblObejctPosition.Text = listText;

                    }


                    //lblObjectAddress.Text = info.HopeAddr == null ? "" : info.HopeAddr.Trim();
                    //lblObejctPosition.Text = info.HopePosition == null ? "" : info.HopePosition.Trim();
                    int hopeMoney = info.HopeSalary.HasValue == true ? info.HopeSalary.Value : -1;
                    lblHopeMoney.Text = MemoryDictionary.GetCommonDictName(hopeMoney);
                    // int _day = info.AvailabilityDay == null ? -1 : info.AvailabilityDay;
                    lblDay.Text = MemoryDictionary.GetCommonDictName(info.AvailabilityDay);
                    lblTel.Text = info.LinkPhone == null ? "" : info.LinkPhone.Trim();
                    lblMobile.Text = info.LinkMobile == null ? "" : info.LinkMobile.Trim();
                    lblQQ.Text = info.QqMsn == null ? "" : info.QqMsn.Trim();
                    lblEmail.Text = info.LinkEmail == null ? "" : info.LinkEmail.Trim();
                    lblIndex.Text = info.HomePage == null ? "" : info.HomePage.Trim();
                    string address = info.ContactAddr == null ? "" : info.ContactAddr;
                    string pcode = info.ContactPostcode == null ? "" : info.ContactPostcode;
                    lblAddress.Text = address + "(邮编: " + pcode + ")";
                }

                lblSelf.Text = pl.GetPersonDesc(pid);
                if (lblSelf.Text.Trim().Length < 1)
                {
                    TRSelfHeader.Visible = false;
                    TRSelfItem.Visible = false;
                }
                lblAttachment.Text = pl.GetPersonOtherDesc(pid);
                if (lblAttachment.Text.Trim().Length < 1)
                {
                    TRAttachmentHeader.Visible = false;
                    TRAttachmentItem.Visible = false;
                }
                JobPersonInfo info1 = pl.GetUserPhoto(pid);
                byte[] photo = info1.UserPhoto == null ? null : info1.UserPhoto;
                string extName = info1.PhotoExtName == null ? "" : info1.PhotoExtName;
                if (photo != null)
                {
                    imgResume.ImageUrl = WebCommon.GetApplicationPath(Request) + "Handler/GetUserPhoto.aspx?personalid=" + pid + "&type=" + extName;
                }

                gvSkill.DataSource = pl.GetSkillList(pid);
                gvSkill.DataBind();
                if (gvSkill.Rows == null || gvSkill.Rows.Count <= 0)
                {
                    TRSkillHeader.Visible = false;
                    TRSkillItem.Visible = false;
                }
                dlCer.DataSource = pl.GetCerList(pid);
                dlCer.DataBind();
                if (dlCer.Items == null || dlCer.Items.Count <= 0)
                {
                    TRCerHeader.Visible = false;
                    TRCerItem.Visible = false;
                }

                dlProject.DataSource = pl.GetProjectListByShow(pid);
                dlProject.DataBind();
                if (dlProject.Items == null || dlProject.Items.Count <= 0)
                {
                    TRProjectHeader.Visible = false;
                    TRProjectItem.Visible = false;
                }

                JobPersonAdvance adv = pl.GetAdvance(pid);
                string advstring = "";
                if (adv != null)
                {
                    if (adv.LeaveTime.HasValue && adv.LeaveTime.Value > 0)
                    {
                        lbAdvLiZhi.Text = MemoryDictionary.GetCommonDictName(adv.LeaveTime.Value);
                    }
                    if (adv.BaseWage.HasValue)
                    {
                        lbAdvBaseSalary.Text = adv.BaseWage.Value + "万元/年";
                    }
                    if (adv.Allowance.HasValue)
                    {
                        lbAdvBuTie.Text = adv.Allowance.Value + "万元/年";
                    }
                    if (adv.Bonus.HasValue)
                    {
                        lbAdvJiang.Text = adv.Bonus.Value + "万元/年";
                    }
                    if (adv.Stock != null && adv.Stock.Length > 0)
                    {
                        lbAdvStock.Text = adv.Stock;
                    }

                    TRAttachmentItem.Visible = true;
                    TRAdvanceHeader.Visible = true;
                }
                else
                {
                    TRAdvanceHeader.Visible = false;
                    TRAdvanceItem.Visible = false;
                }

                if (new MySession().GetAdminUserFromSession() != null)
                {
                    this.adminTR0.Visible = true;
                    string personCloass = string.Empty;
                    if (info.PersonClass.HasValue) personCloass = info.PersonClass.Value.ToString();
                    WebCommon.SetDropDownFromDict(ddlPersonClass, personCloass,
                        Constants.GetDictionaryCode(Constants.DictionaryType.PERSON_CLASS), false);

                    this.btDelete.Attributes.Add("onclick", "return confirm('是否确认删除本简历？');");
                }
                else
                {
                    this.adminTR0.Visible = false;
                }
            }
            else
            {
                AHRBROWSE.InnerText = "中文简历预览";
                AHRBROWSE.HRef = "ResumeBrowse.aspx?resumeid=" + this.ResumeId + "&categoryid=1&resumetype=1";
                this.adminTR0.Visible = false;
                TRCHS.Visible = false;
                TDENG.InnerHtml = Functions.TextAreaEncode( pl.GetPersonForeignResume(pid));
            }

        }

        protected void gvSkill_RowCommand(object sender, GridViewCommandEventArgs e)
        {

        }

        protected void gvSkill_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void gvSkill_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblLevelName = (Label)e.Row.FindControl("lblLevelName");
                Label lblLevel = (Label)e.Row.FindControl("lblLevel");
                Label lblSKillName = (Label)e.Row.FindControl("lblSKillName");
                Label lblName = (Label)e.Row.FindControl("lblName");
                Label lbluserlen = (Label)e.Row.FindControl("lbluserlen");
                Label lbluselenName = (Label)e.Row.FindControl("lbluselenName");

                int level = 0;
                Int32.TryParse(lblLevel.Text.Trim(), out level);

                int uselen = 0;
                Int32.TryParse(lbluserlen.Text.Trim(), out uselen);

                lblLevelName.Text = MemoryDictionary.GetCommonDictName(level);
                lblSKillName.Text = MemoryDictionary.GetFullItAbilityName(lblName.Text.Trim());
                //lbluselenName.Text = MemoryDictionary.GetCommonDictName(uselen);
                if (lbluserlen.Text != null && lbluserlen.Text.Length > 0)
                    lbluselenName.Text = lbluserlen.Text + " 月";
            }
        }

        protected void dlCer_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void ShowError(string message)
        {
            this.msgTd.InnerHtml = "<font color='red'>" + message + "</font>";
            this.msgTd.Visible = true;
            this.msgTd.VAlign = "middle";
        }

        protected void btAuth_Click(object sender, EventArgs e)
        {
            MySession session = new MySession();
            if (session.GetAdminUserFromSession() == null)
            {
                ShowError("您无权设置个人简历内部分类！");
            }
            else
            {
                try
                {
                    int personClass = Convert.ToInt32(ddlPersonClass.SelectedValue);
                    new PersonalLogic().UpdatePersonClass(ResumeId, personClass);
                    ShowError("已经设置个人简历内部分类为：" + ddlPersonClass.SelectedItem.Text + "！");
                }
                catch (Exception ex)
                {
                    ShowError("设置个人简历内部分类失败：" + ex.Message);
                }

            }
        }

        protected void btDelete_Click(object sender, EventArgs e)
        {
            MySession session = new MySession();
            if (session.GetAdminUserFromSession() == null)
            {
                ShowError("您无权删除个人简历！");
            }
            else
            {
                try
                {
                    new PersonalLogic().UpdatePersonStatus(ResumeId, 0);
                    ReturnError("删除个人简历成功！");
                }
                catch (Exception ex)
                {
                    ShowError("删除个人简历失败：" + ex.Message);
                }
            }
        }
    }
}